package com.lookinto.applet;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Graphics;
import java.awt.GridBagConstraints;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.StringReader;
import java.io.StringWriter;
import java.util.regex.Pattern;
import javax.swing.JApplet;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;
import javax.swing.text.DefaultStyledDocument;
import javax.swing.text.rtf.RTFEditorKit;

/**
 *
 * @author admin
 */
public class RTFApplet extends JApplet {

  private JTextPane textpane = new JTextPane();
  RTFEditorKit kit;
  DefaultStyledDocument doc;
  protected String rtf;

  /**
   * Get the value of rtf
   *
   * @return the value of rtf
   */
  public synchronized String getRtf() {
    return rtf;
  }

  /**
   * Set the value of rtf
   *
   * @param rtf new value of rtf
   */
  public synchronized void setRtf(String rtf) {
    this.rtf = rtf;
  }
  private int fontSize = -1;

  public void init() {
    final String url = getParameter("url");
    final String query = getParameter("query");
    Container contentPane = getContentPane();
    JButton plus = new ButtonTest("+");
    JButton minus = new ButtonTest("-");
    //plus.setFont(plus.getFont().getSize());
    plus.addActionListener(
            new java.awt.event.ActionListener() {

              public void actionPerformed(ActionEvent ae) {
                if (fontSize == -1) {
                  fontSize = textpane.getFont().getSize();
                }

                javax.swing.Action ac[] = kit.getActions();
                for (javax.swing.Action a : ac) {
                  System.out.println(a.toString() + ".." + a.getValue(a.ACTION_COMMAND_KEY));
                  if (a.toString().indexOf("All") > 0) {
                    a.actionPerformed(new ActionEvent(textpane, ActionEvent.ACTION_PERFORMED, null));
                  }
                }
                fontSize += 3;
                (new RTFEditorKit.FontSizeAction("font-size-" + fontSize, fontSize)).actionPerformed(new ActionEvent(textpane, ActionEvent.ACTION_PERFORMED, null));

                for (javax.swing.Action a : ac) {
                  //System.out.println(a.toString() +".."+ a.getValue(a.ACTION_COMMAND_KEY));
                  if (a.toString().indexOf("BeginAction") > 0) {
                    a.actionPerformed(new ActionEvent(textpane, ActionEvent.ACTION_PERFORMED, null));
                    System.out.println(kit.beginAction + " ");
                    break;
                  }
                }
              }
            });

    minus.addActionListener(new java.awt.event.ActionListener() {

      public void actionPerformed(ActionEvent ae) {
        if (fontSize == -1) {
          fontSize = textpane.getFont().getSize();
        }

        javax.swing.Action ac[] = kit.getActions();
        for (javax.swing.Action a : ac) {
          System.out.println(a.toString() + ".." + a.getValue(a.ACTION_COMMAND_KEY));
          if (a.toString().indexOf("All") > 0) {
            a.actionPerformed(new ActionEvent(textpane, ActionEvent.ACTION_PERFORMED, null));
          }
        }
        fontSize -= 3;
        (new RTFEditorKit.FontSizeAction("font-size-" + fontSize, fontSize)).actionPerformed(new ActionEvent(textpane, ActionEvent.ACTION_PERFORMED, null));

        for (javax.swing.Action a : ac) {
          //System.out.println(a.toString() +".."+ a.getValue(a.ACTION_COMMAND_KEY));
          if (a.toString().indexOf("BeginAction") > 0) {
            a.actionPerformed(new ActionEvent(textpane, ActionEvent.ACTION_PERFORMED, null));
            System.out.println(kit.beginAction + " ");
            break;
          }
        }
      }
    });
    JPanel jp = new JPanel();
    jp.setLayout(new java.awt.GridBagLayout());
    GridBagConstraints cc = new GridBagConstraints();
    //cc.ipadx = 5;
    cc.anchor = GridBagConstraints.NORTHEAST;
    cc.insets = new Insets(0, 5, 0, 0);
    //jp.setBackground(Color.red);
    jp.add(plus, cc);
    jp.add(minus, cc);
    JPanel damn = new JPanel();
    damn.setLayout(new BorderLayout());
    damn.add(jp, BorderLayout.WEST);
    contentPane.add(damn, BorderLayout.NORTH);
    contentPane.add(new JScrollPane(textpane),
            BorderLayout.CENTER);

    textpane.setContentType("text/rtf");
    kit = (RTFEditorKit) textpane.getEditorKitForContentType("text/rtf");
    doc = (DefaultStyledDocument) kit.createDefaultDocument();

    textpane.setEditorKit(kit);
    textpane.setDocument(doc);

    // doc.
    Thread t = new Thread() {

      public void run() {
        try {
          while (getRtf() == null) {
            Thread.currentThread().sleep(500);
          }
          String result = highlight(getRtf(), query);
          kit.read(new StringReader(result), doc, 0);

        } catch (Exception ex) {
          ex.printStackTrace();
        }

      }
    };
    t.start();
    try {
      rtf = highlight(test(), "snow");
      (new RTFEditorKit.FontSizeAction("font-size-" + 100, 100)).actionPerformed(new ActionEvent(textpane, ActionEvent.ACTION_PERFORMED, null));
    } catch (Exception e) {
    }
  }

  private String highlight(String rtfString, String keyWords) {
    String result = rtfString;
    if (keyWords != null) {
      String[] terms = keyWords.split(" ");
      for (String term : terms) {
        Pattern p = Pattern.compile("\\b" + term + "\\b", Pattern.CASE_INSENSITIVE);
        result = (p.matcher(result).replaceAll("{\\\\cf5\\\\highlight7 $0}"));
      }
    }
    return result;
  }

  private String test() throws Exception {
    BufferedReader br;

    String fname = "c:\\docs\\snow.rtf";
    File file = new File(fname);

    FileReader sr = new FileReader(file);
    br = new BufferedReader(sr);

    StringWriter sw = new StringWriter();
    String line;
    while ((line = br.readLine()) != null) {
      sw.write(line);
    }
    ;
    br.close();
    return sw.toString();
  }
}

class ButtonTest extends JButton {

  public ButtonTest(String title) {
    super(title);
    setContentAreaFilled(false);
  }

  protected void paintComponent(Graphics g) {
    g.setColor(Color.lightGray);
    g.fillOval(0, 0, getSize().width - 1, getSize().height - 1);
    super.paintComponent(g);
  }

  protected void paintBorder(Graphics g) {
    g.setColor(Color.green);
    g.drawOval(0, 0, getSize().width - 1, getSize().height - 1);
  }
}
